﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SuperX.Common.Helper
{
    /// <summary>
    /// 异步Task类
    /// </summary>    
    public static class AsyncUtil
    {
        /// <summary>
        /// 运行指定task
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>        
        public static Task AsyncRun(Action task)
        {
            return AsyncUtil.AsyncRun(task, TaskCreationOptions.None);
        }

        /// <summary>
        /// 运行指定task
        /// </summary>
        /// <param name="task"></param>
        /// <param name="taskOption"></param>
        /// <returns></returns>
        public static Task AsyncRun(Action task, TaskCreationOptions taskOption)
        {
            return AsyncUtil.AsyncRun(task, taskOption, null);
        }

        /// <summary>
        /// 运行指定task
        /// </summary>
        /// <param name="task"></param>
        /// <param name="exceptionHandler"></param>
        /// <returns></returns>
        public static Task AsyncRun(Action task, Action<Exception> exceptionHandler)
        {
            return AsyncUtil.AsyncRun(task, TaskCreationOptions.None, exceptionHandler);
        }

        /// <summary>
        /// 运行指定task
        /// </summary>
        /// <param name="task">任务</param>
        /// <param name="taskOption">配置项</param>
        /// <param name="exceptionHandler">异常事件</param>
        /// <returns></returns>        
        public static Task AsyncRun(Action task, TaskCreationOptions taskOption, Action<Exception> exceptionHandler)
        {
            return Task.Factory.StartNew(task, taskOption).ContinueWith(delegate (Task t)
            {
                if (exceptionHandler != null)
                {
                    exceptionHandler(t.Exception);
                }
            }, TaskContinuationOptions.OnlyOnFaulted);
        }

        /// <summary>
        /// 运行指定task
        /// </summary>
        /// <param name="task">任务</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        public static Task AsyncRun(Action<object> task, object state)
        {
            return AsyncUtil.AsyncRun(task, state, TaskCreationOptions.None);
        }

        /// <summary>
        /// 运行指定task
        /// </summary>
        /// <param name="task">任务</param>
        /// <param name="state">状态</param>
        /// <param name="taskOption">异常事件</param>
        /// <returns></returns>
        public static Task AsyncRun(Action<object> task, object state, TaskCreationOptions taskOption)
        {
            return AsyncUtil.AsyncRun(task, state, taskOption, null);
        }

        /// <summary>
        /// 运行指定task
        /// </summary>
        /// <param name="task">任务</param>
        /// <param name="state">状态</param>
        /// <param name="exceptionHandler">异常事件</param>
        /// <returns></returns>
        public static Task AsyncRun(Action<object> task, object state, Action<Exception> exceptionHandler)
        {
            return AsyncUtil.AsyncRun(task, state, TaskCreationOptions.None, exceptionHandler);
        }

        /// <summary>
        /// 运行指定task
        /// </summary>
        /// <param name="task">任务</param>
        /// <param name="state">任务状态</param>
        /// <param name="taskOption">配置项</param>
        /// <param name="exceptionHandler">异常事件</param>
        /// <returns></returns>
        public static Task AsyncRun(Action<object> task, object state, TaskCreationOptions taskOption, Action<Exception> exceptionHandler)
        {
            return Task.Factory.StartNew(task, state, taskOption).ContinueWith(delegate (Task t)
            {
                if (exceptionHandler != null)
                {
                    exceptionHandler(t.Exception);
                }
            }, TaskContinuationOptions.OnlyOnFaulted);
        }
    }
}
